<template>
  <div class="checkout-address">
    <div class="text">
      <!-- <div class="none">您需要先添加收货地址才可提交订单。</div> -->
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ addressObj.name }}
        </li>
        <li><span>联系方式：</span>{{ addressObj.tel }}</li>
        <li><span>收货地址：</span>{{ addressObj.site }}</li>
      </ul>
      <!-- <a href="javascript:;">修改地址</a> -->
    </div>
    <div class="action">
      <el-button class="btn" type="success" @click="editAddress">修改地址</el-button>
      <el-button class="btn" type="primary" @click="plusAddress">添加地址</el-button>
    </div>
    <!-- 地址框弹层 -->
    <el-dialog :title="switchBox === 1 ? '修改地址' : '添加地址'" :visible.sync="visible" width="30%" center>
      <span></span>
      <el-form :model="addressForm" label-width="100px">
        <el-form-item label="收货人">
          <el-input type="text" v-model="addressForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input type="text" v-model="addressForm.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收货地址">
          <el-input type="text" v-model="addressForm.site"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="sendAddress">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getUserAddress, updateAddress, insertAddress } from '@/api/user'
export default {
  name: 'Address',
  data() {
    return {
      addressObj: {},
      visible: false,
      addressForm: {
        name: '',
        tel: '',
        site: '',
      },
      switchBox: null, // 1为修改地址 2为添加地址
    }
  },
  created() {
    // const uid = this.$store.state.user.uid
    // getUserAddress({ uid }).then((result) => (this.addressObj = result.data[0]))
    this.initAddress()
  },
  methods: {
    initAddress() {
      const uid = this.$store.state.user.uid
      getUserAddress({ uid }).then((result) => (this.addressObj = result.data[0]))
    },
    editAddress() {
      this.visible = true
      this.switchBox = 1
    },
    plusAddress() {
      this.visible = true
      this.switchBox = 2
    },
    async sendAddress() {
      const uid = this.$store.state.user.uid
      if (this.switchBox === 1) {
        // 提交修改地址
        const result = await updateAddress({ ...this.addressForm, uid })
        // console.log(result)
        if (result.status != 0) return this.$message.error('修改地址失败！')
        this.$message.success('修改地址成功！')
        this.visible = false
        this.addressForm.name = ''
        this.addressForm.tel = ''
        this.addressForm.site = ''
        this.initAddress()
      } else {
        if (addressObj === null) {
          const result = await insertAddress({ ...this.addressForm, uid })
          if (result.status != 0) return this.$message.error('添加地址失败！')
          this.$message.success('添加地址成功！')
          this.visible = false
          this.addressForm.name = ''
          this.addressForm.tel = ''
          this.addressForm.site = ''
          this.initAddress()
        } else {
          this.$message.error('添加地址失败！')
        }
      }
    },
  },
}
</script>
<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        list-style: none;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: #27ba9b;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 23px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
